﻿using System;
using System.Collections.Generic;

// 形状接口
public interface IShape
{
    double CalculateArea();  
    bool IsValid();     
}

// 形状基类
public abstract class Shape : IShape
{
    public abstract double CalculateArea();
    public abstract bool IsValid();
}

// 长方形类
public class Rectangle : Shape
{
    public double Length { get; set; }
    public double Width { get; set; }
    public Rectangle(double length, double width)
    {
        Length = length;
        Width = width;
    }
    //计算面积与是否合法
    public override double CalculateArea()
    {
        if (!IsValid()) return 0;
        return Length * Width;
    }
    public override bool IsValid()
    {
        return Length > 0 && Width > 0;
    }

    public override string ToString()
    {
        return $"长方形[长={Length}, 宽={Width}]";
    }
}

// 正方形类
public class Square : Shape
{
    public double Side { get; set; }
    public Square(double side)
    {
        Side = side;
    }
    //计算面积与是否合法
    public override double CalculateArea()
    {
        if (!IsValid()) return 0;
        return Side * Side;
    }
    public override bool IsValid()
    {
        return Side > 0;
    }

    public override string ToString()
    {
        return $"正方形[边长={Side}]";
    }
}

// 圆形类
public class Circle : Shape
{
    public double Radius { get; set; }

    public Circle(double radius)
    {
        Radius = radius;
    }
    //计算面积与是否合法
    public override double CalculateArea()
    {
        if (!IsValid()) return 0;
        return Math.PI * Radius * Radius;
    }
    public override bool IsValid()
    {
        return Radius > 0;
    }
    public override string ToString()
    {
        return $"圆形[半径={Radius}]";
    }
}

class Program
{
    static void Main(string[] args)
    {
        // 随机创建10个形状对象
        List<Shape> shapes = GenerateRandomShapes(10);
        Console.WriteLine("生成的形状:");
        foreach (var shape in shapes)
        {
            Console.WriteLine($"{shape}:\n面积={shape.CalculateArea():F2}, 是否有效={shape.IsValid()}");
        }

        // 计算总面积
        double totalArea = CalculateTotalArea(shapes);
        Console.WriteLine($"\n所有形状的总面积为: {totalArea:F2}");
        Console.ReadKey();
    }

    // 随机生成形状函数
    static List<Shape> GenerateRandomShapes(int count)
    {
        List<Shape> shapes = new List<Shape>();
        Random random = new Random();
        for (int i = 0; i < count; i++)
        {
            int type = random.Next(0, 3); // 0=长方形, 1=正方形, 2=圆形
            double size1 = random.Next(1, 10) + random.NextDouble();
            double size2 = random.Next(1, 10) + random.NextDouble();
            Shape shape = null;
            switch (type)
            {
                case 0:
                    shape = new Rectangle(size1, size2);
                    break;
                case 1:
                    shape = new Square(size1);
                    break;
                case 2:
                    shape = new Circle(size1);
                    break;
            }
            shapes.Add(shape);
        }
        return shapes;
    }

    // 计算所有形状的总面积
    static double CalculateTotalArea(List<Shape> shapes)
    {
        double total = 0;
        foreach (var shape in shapes)
        {
            total += shape.CalculateArea();
        }
        return total;
    }
}